/**
 * 设置料号生效的
 */
package ext.tianma.part.processors;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import wt.part.WTPart;
import wt.session.SessionServerHelper;
import wt.util.WTException;

import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.generic.part.CusIBAUtil;

public class UpdateEffectPartNumberProcessors extends DefaultObjectFormProcessor{
	//物料生效地
	private static final String PART_NUMBER_EFFECTIVE_SITE = "partNumberEffectiveSite" ;
	
	@Override
	public FormResult doOperation(NmCommandBean nmCommandBean, List list)throws WTException {
		
		FormResult formresult = new FormResult(FormProcessingStatus.SUCCESS);
		
		NmOid nmoid = nmCommandBean.getActionOid();
		
		if( nmoid != null ){			
			WTPart currentPart = (WTPart) nmoid.getRefObject();
			
			if( currentPart != null ){
				HttpServletRequest request = nmCommandBean.getRequest();
				
				if( request != null ){
					String[] factorys = request.getParameterValues("factory");
					System.out.println( "Debug   factorys:"+factorys );
					if( factorys != null ){
						boolean isSuccess = setPartNumberEffectiveSite( currentPart , factorys );
						
						if( isSuccess ){
							formresult.addFeedbackMessage( new  FeedbackMessage(
									FeedbackType.SUCCESS, null, "料号生效地修改成功", null, new String[] {} ) );
						}
					}
				}
			}			
		}		
		return formresult;
	}
	
	/**
	 * 设置物料生效地
	 * @param currentPart
	 * @param factorys
	 * @throws WTException 
	 */
	private boolean setPartNumberEffectiveSite( WTPart currentPart , String[] factorys ) throws WTException {
		boolean isSuccess = false;
		
		if( currentPart == null || factorys == null ){
			return isSuccess;
		}
		
		String partNumberEffectiveSite = "";
		
		for (int i = 0; i < factorys.length; i++) {
			
			if( factorys[i] != null ){
				
				if( partNumberEffectiveSite.equals( "" ) ){
					partNumberEffectiveSite = factorys[i] ;
				}else{
					partNumberEffectiveSite = partNumberEffectiveSite +","+factorys[i];
				}
			}
		}
		
		boolean flag = SessionServerHelper.manager.setAccessEnforced(false);
		try{
			CusIBAUtil.forceSetEnumIBAValue( currentPart , PART_NUMBER_EFFECTIVE_SITE , partNumberEffectiveSite );
			isSuccess = true;
		}finally{
			SessionServerHelper.manager.setAccessEnforced( flag );
		}
		
		return isSuccess ;
	}

}
